package cn.zou.blog.controller;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.junit.internal.matchers.SubstringMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.zou.blog.entry.Article;
import cn.zou.blog.service.ArticleService;
import cn.zou.core.page.Page;

@Controller
@RequestMapping("/login")
public class LoginController {
	@Autowired
	private ArticleService articleService;
	@RequestMapping("/admin/index.shtml")
	public String indexUI(@RequestParam("username") String  username,@RequestParam("password") String password,HttpServletRequest request,HttpServletResponse  response,ModelMap map){
       Subject subject = SecurityUtils.getSubject();
       UsernamePasswordToken token=new UsernamePasswordToken(username, password);
       try {
		subject.login(token);
	} catch (AuthenticationException e) {
		try {
			map.addAttribute("username", username);
			map.addAttribute("password", password);
			map.addAttribute("errorInfo", "用户名密码错误");
			response.sendRedirect(request.getContextPath()+"/login.jsp");
			
		} catch (IOException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		e.printStackTrace();
	}
       
		return "admin/Admin_index";
	}
	@RequestMapping("/indexUI.shtml")
	public String toindexUI(Page<Article> page,HttpServletRequest request,ModelMap map){
		//查询所所有文章
		page = articleService.findArticles(page,request,"1","all");
		map.addAttribute("page", page);
		map.addAttribute("articletype", Article.INFO_TYPE_MAP);
		return "home";
	}
}
